﻿using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using TeamsPortal.DataAccess;
using TeamsPortal.ServiceClient;
using TeamsPortal.Tests.Controllers;
using Moq;

namespace TeamsPortal.Tests
{
	[TestFixture]
	public class ServicesTests
	{
		[Test]
		public void UsersServiceClient_Add_Get_Delete()
		{
			var user = new User(0, "UNITTEST USERNAME EMPTY");
			user.Role = UserRole.Member;
			string providerId = "00" + new Random().Next(int.MaxValue / 2, int.MaxValue).ToString();
            var memberShipServiceMock = new Mock<IMembershipService>();
            var client = new UsersServiceClient(memberShipServiceMock.Object);

            client.Add(user, AuthenticationProvider.Twitter, providerId);

            user = client.GetByProviderId(AuthenticationProvider.Twitter, providerId);
			Assert.IsTrue(user.Id > 0);

            client.Delete(user.Id);

            user = client.GetByProviderId(AuthenticationProvider.Twitter, providerId);

			Assert.IsNull(user);

			//Perform the same test but with all properties populated.
			user = new User(0, "UNITTEST USERNAME FULL");
			user.Role = UserRole.Member;
			user.BirthDate = new DateTime(1960, 12, 31);
			user.ExternalProfileUrl = "http://twitter.com/jorgebg";
			//user.Photo = "http://twitter.com/jorgebg.png";
			user.Profile = "Hello world1";
			user.Signature = "Hello world2";
			user.TimeZone = new TimeSpan(2, 0, 0);
			user.Website = "http://twitter.com/jorgebg/";

			providerId = "00" + new Random().Next(int.MaxValue / 2, int.MaxValue).ToString();

            client.Add(user, AuthenticationProvider.Twitter, providerId);

            user = client.GetByProviderId(AuthenticationProvider.Twitter, providerId);
			Assert.IsTrue(user.Id > 0);

            client.Delete(user.Id);

            user = client.GetByProviderId(AuthenticationProvider.Twitter, providerId);
			Assert.IsNull(user);
		}

		[Test]
		public void TopicsSubscriptions_Add_Get_Delete_Test()
		{
            var topicsSubscriptionClient = new TopicsSubscriptionsServiceClient();
			var user = GetTestUser();
			var forum = ForumsControllerTest.GetAForum();
			var topic = TopicsControllerTest.GetATopic(forum);

            topicsSubscriptionClient.Add(topic.Id, user.Id);

            var subscribedUsers = topicsSubscriptionClient.GetSubscribed(topic.Id);
            var subscribedTopics = topicsSubscriptionClient.GetTopics(user.Id);

			Assert.IsTrue(subscribedUsers.Count > 0);
			Assert.IsTrue(subscribedTopics.Count > 0);

			//Check that the topic recently subscribed is present.
			Assert.IsTrue(subscribedTopics.Any(x => x.Id == topic.Id));

			NotificationsServiceClient.SendToUsersSubscribed(topic, subscribedUsers, "Unit test email from " + MethodInfo.GetCurrentMethod().Name + ", :<!--!UNSUBSCRIBEURL!-->:<!--!URL!-->:<!--!TITLE!-->:", "http://url", "http://unsubscribeUrl/{0}/{1}", false);

            topicsSubscriptionClient.Remove(topic.Id, user.Id, user.Guid);

            subscribedTopics = topicsSubscriptionClient.GetTopics(user.Id);

			//Check that the topic recently unsubscribed is NOT present.
			Assert.IsFalse(subscribedTopics.Any(x => x.Id == topic.Id));
		}

		public static User GetTestUser()
		{
			User user = new UsersServiceClient(null).GetTestUser();
			if (user == null)
			{
				Assert.Inconclusive("There is no user in the db.");
			}
			return user;
		}
	}
}
